package com.company.cloud.user.modules.mgr.controller;

import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.user.modules.api.constant.UserOneTypeEnum;
import com.company.cloud.user.modules.app.models.UserCountDetail;
import com.company.cloud.user.modules.app.models.UserCountModel;
import com.company.cloud.user.modules.cache.models.TeamModel;
import com.company.cloud.user.modules.mgr.controller.req.UserCountParam;
import com.company.cloud.user.modules.mgr.service.UserCountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

@Log4j2
@Authc
@RestController
@RequestMapping("/mgr/admin/dataUser")
@Api(tags="User_Mgr_用户数据管理")
public class UserDataMgrController {

    @Autowired
    private UserCountService userCountService;

    @PostMapping("/userCount")
    @ApiOperation(value="用户数量", response= TeamModel.class)
    public BaseResult customerCount(@RequestBody UserCountParam params) {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        String companyNo = userInfo.getCompanyNo();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String startDate = df.format(LocalDateTime.now().minusDays(params.getGetDays()).with(LocalTime.MIN));
        String endDate = df.format(LocalDateTime.now());
        UserCountModel userCountModel = new UserCountModel();
        // 员工数量统计
        if (BaseConstant.YNEnum.YES.getCode().equals(params.getEmployee())) {
            UserCountDetail employeeCountDetail = userCountService.getUserCountDetail(UserOneTypeEnum.Employee_User.getCode(),
                    startDate, endDate, companyNo, BaseConstant.YNEnum.YES.getCode().equals(params.getGetAll()), null);
            userCountModel.setEmployee(employeeCountDetail);

            if (BaseConstant.YNEnum.YES.getCode().equals(params.getGetMyData())) {
                UserCountDetail myEmployeeCountDetail = userCountService.getUserCountDetail(UserOneTypeEnum.Employee_User.getCode(),
                        startDate, endDate, companyNo, BaseConstant.YNEnum.YES.getCode().equals(params.getGetAll()), userInfo);
                userCountModel.setMyEmployee(myEmployeeCountDetail);
            }
        }
        // 客户数量统计
        if (BaseConstant.YNEnum.YES.getCode().equals(params.getCustomer())) {
            UserCountDetail employeeCountDetail = userCountService.getUserCountDetail(UserOneTypeEnum.Customer_User.getCode(),
                    startDate, endDate, companyNo, BaseConstant.YNEnum.YES.getCode().equals(params.getGetAll()), null);
            userCountModel.setCustomer(employeeCountDetail);
            if (BaseConstant.YNEnum.YES.getCode().equals(params.getGetMyData())) {
                UserCountDetail myEmployeeCountDetail = userCountService.getUserCountDetail(UserOneTypeEnum.Customer_User.getCode(),
                        startDate, endDate, companyNo, BaseConstant.YNEnum.YES.getCode().equals(params.getGetAll()), userInfo);
                userCountModel.setMyCustomer(myEmployeeCountDetail);
            }
        }



        return BaseResult.ok(userCountModel);
    }
}
